package com.wy.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sun.org.apache.xpath.internal.operations.Or;
import com.wy.entity.*;
import com.wy.service.IDrugService;
import com.wy.service.IOrderDetailService;
import com.wy.service.IOrderService;
import com.wy.utils.CommonUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wy.common.R;

import com.wy.service.IShopCartService;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 王昭贵
 * @since 2023-01-27
 */
@RestController
@RequestMapping("/shopcart")
public class ShopCartController {

    @Resource
    private IShopCartService shopCartService;
    @Resource
    private IOrderDetailService orderDetailService;
    @Resource
    private IOrderService orderService;
    @Resource
    private IDrugService drugService;

    // 新增或者更新
    @PostMapping
    public R save(@RequestBody ShopCart shopCart) {
        shopCartService.saveOrUpdate(shopCart);
        return R.success("");
    }

    /****
     * 购物车--复选框选中改变状态
     * @param ids
     * @return
     */
    @PostMapping("/changeSelectShop")
    public R changeSelectShop(@RequestBody int[] ids) {
        //后面再  优化
        List<ShopCart> list = shopCartService.list();
        for (ShopCart shopcart : list) {
            shopcart.setSelected(0);
            shopCartService.updateById(shopcart);
        }
        for (int id : ids) {
            ShopCart shopCart = new ShopCart();
            shopCart.setId(id);
            shopCart.setSelected(1);
            shopCartService.updateById(shopCart);
        }
        return R.success("");
    }

    /****
     * 加入购物车 并将状态置为1表示已选中
     * @param shopCart
     * @return
     */
    @PostMapping("/addShopCart")
    public R addShopCart(@RequestBody ShopCart shopCart) {
        shopCart.setSelected(1);
        shopCartService.addShopCart(shopCart);
        return R.success("");
    }

    /****
     * 前台购物车提交订单
     * @param customerId
     * @return
     */
    @Transactional
    @GetMapping("/shopCartToOrderByCustomerId/{customerId}")
    public R finalOrder(@PathVariable Integer customerId) {
        LambdaQueryWrapper<ShopCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShopCart::getCustomerId, customerId).eq(ShopCart::getSelected, 1);
        List<ShopCart> list = shopCartService.list(queryWrapper);
        List<Integer> collect = list.stream().map(item -> item.getId()).collect(Collectors.toList());
        double sumPrice = 0;
        for (Integer id : collect) {
            queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ShopCart::getId, id);
            ShopCart one = shopCartService.getOne(queryWrapper);
            Drug drug = drugService.getById(one.getDrugId());
            sumPrice += one.getCount() *drug.getPrice();
        }
        //1.生成订单--订单表--药品
        Order order = new Order();
        order.setCode(CommonUtils.customFormatDate("yyyyMMddHHmmssSSSSSSS"));
        order.setCustomerId(customerId);
        order.setPayType(0);
        order.setPayPrice(BigDecimal.valueOf(sumPrice));
        order.setPayStatus(0);
        orderService.save(order);
        //2.生成订单明细
        for (Integer id : collect) {
            queryWrapper = new LambdaQueryWrapper<>();
            ShopCart one = shopCartService.getOne(queryWrapper.eq(ShopCart::getId, id));
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setOrderId(order.getId());
            orderDetail.setSourceType(1);
            orderDetail.setCount(one.getCount());//暂时设定药品默认数量为1
            orderDetail.setSourceId(one.getDrugId());
            orderDetail.setSinglePrice(drugService.getById(one.getDrugId()).getPrice());
            orderDetailService.save(orderDetail);
        }
        //3.清空购物车
        shopCartService.removeByIds(collect);
        return R.success(order.getCode());
    }

    /****
     * 删除购物车
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public R delete(@PathVariable Integer id) {
        shopCartService.removeById(id);
        return R.success("");
    }

    /****
     * 获取购物车列表 根据用户id
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R findOne(@PathVariable Integer id) {
        List<ShopCart> shopCartList = shopCartService.getByCustomerId(id);
        return R.success(shopCartList);
    }



}

